import router from '../../routes'
export function controller(root) {
  return function (target) {
    const { constructor, ...methods } = Object.getOwnPropertyDescriptors(target.prototype)
    for (let key in methods) {
      const path = Reflect.getMetadata('path', target.prototype, key)
      const method = Reflect.getMetadata(
        'method',
        target.prototype,
        key
      )
      const middlewares = Reflect.getMetadata(
        'middlewares',
        target.prototype,
        key
      )
      const fullPath = root === '/' ? path : `${root}${path}`
      if (path && method) {
        if (middlewares && middlewares.length) {
          router[method](fullPath, ...middlewares, target.prototype[key])
        } else {
          router[method](fullPath, target.prototype[key])
        }
      }
    }
  }
}
